var app = getApp()
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');

var shareContent = {
    'title':'企鹅优品－匠心比心',
    'desc':'腾讯精品体验平台，为用户提供优质的生活方式和购物体验。',
    'path':'/pages/new-index/index'
}
var shareObj = utils.share(shareContent);
var pageSetting = {
    data: {
        list: [],
        readyGifList: {},
        cartNum: 0
    },
    clearGif: function() {
        // 删除旧代码残留的gif文件
        wx.getSavedFileList({
          success: function(res) {
            if (res.fileList.length > 0){
                for(var k = 0; k < res.fileList.length; k ++){
                    if(res.fileList[k].filePath && res.fileList[k].filePath.endsWith('gif')){
                        wx.removeSavedFile({
                            filePath: res.fileList[k].filePath
                        });
                    }
                }
            }
          }
        });
    },
    gifReady: function(e) {
        var gif = e.target.dataset.gif;
        var readyGifList = this.data.readyGifList;
        readyGifList[gif] = gif;

        this.setData({
            readyGifList: readyGifList
        });
    },
    onLoad: function() {
        app.globalData.cartNumState = false;
        var that = this;
        // 请求数据
        modal.loading();

        request.post({
            path: '/wxapp_api/api'
        },{
            "yp:api:article:homePage":{}
        })
        .then(function (res){
            var homePage = res['yp:api:article:homePage'];
            if(homePage.ret != 0){
                // modal.toast(homePage.msg);
                wx.navigateTo({
                    url: "../error/error"
                })
                return false;
            }
            var list = homePage.data.homePage;
            // 设置data
            that.setData({
                list: list
            });
            modal.hide();
        }, function(res){
            modal.hide();
            modal.toast("请求失败，请检查您的网络", "loading");
        });
        request.post({
            path: '/wxapp_api/api'
        },{'self:cart_api:cart:cartcount':{}})
        .then(function(res){
            var cartData = res['self:cart_api:cart:cartcount'];
            var cartNum = that.data.cartNum;
            if(cartData.ret != 0){
                // modal.toast(cartData.msg);
                wx.navigateTo({
                    url: "../error/error"
                })
                return false;
            }
            cartNum = cartData.data;
            that.setData({
                cartNum : cartNum
            });
        })

        // 删除旧代码放在本地的gif，当前实现不再使用saveFile方式
        var hasClearGif = wx.getStorageSync('hasClearGif');
        if(!hasClearGif){
            wx.setStorageSync('hasClearGif', '1');
            this.clearGif();
        }
    },
    onShow : function(e){
        var that = this;
        if(app.globalData.cartNumState){
            app.globalData.cartNumState = false;
            request.post({
                path: '/wxapp_api/api'
            },{'self:cart_api:cart:cartcount':{}})
            .then(function(res){
                var cartData = res['self:cart_api:cart:cartcount'];
                var cartNum = that.data.cartNum;
                if(cartData.ret != 0){
                    // modal.toast(cartData.msg);
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }
                cartNum = cartData.data;
                that.setData({
                    cartNum : cartNum
                });
            })
        }
    },
    goCartPage: function(e) {
        request.bind().then(() => {
            wx.navigateTo({
                url: '../shopping-cart/shopping-cart'
            })
        })
    }
};

utils.assign(pageSetting,shareObj);
Page(pageSetting);